<?php
/*
 * @Author: an
 * @Date: 2021/12/9
 * @Description: 直播流管理
 */


namespace App\Services\Aliyun\Live;


use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use App\Services\Aliyun\BaseService;
use App\Services\Configs\DefaultService;

class StreamsService extends BaseService
{
    private $accessKeyId;//阿里云添加用户生成
    private $accessKeySecret;//阿里云添加用户生成

    public function __construct()
    {
        $config = $this->getConfig(DefaultService::ALI_LIVE);
        $this->accessKeyId = $config['accessKeyId'];
        $this->accessKeySecret = $config['accessKeySecret'];
    }

    /**
     * 获取域名在线流列表
     * @param $DomainName 流域名
     * @param $PageNum 分页
     * @return array|string
     * @throws ClientException
     * @author an
     * @time 2021-12-09 14:30
     */
    public function streamsOnlineList($DomainName, $PageNum)
    {
        AlibabaCloud::accessKeyClient($this->accessKeyId, $this->accessKeySecret)
            ->regionId('cn-qingdao')
            ->asDefaultClient();

        try {
            $result = AlibabaCloud::rpc()
                ->product('live')
                // ->scheme('https') // https | http
                ->version('2016-11-01')
                ->action('DescribeLiveStreamsOnlineList')
                ->method('POST')
                ->host('live.aliyuncs.com')
                ->options([
                    'query' => [
                        'DomainName' => $DomainName,//"play.xxxxxx.com.cn"
                        'PageNum' => $PageNum,
                    ],
                ])
                ->request();
            return $result->toArray();
        } catch (ClientException $e) {
            return $e->getErrorMessage();
        } catch (ServerException $e) {
            return $e->getErrorMessage();
        }
    }
}
